<?php

/**
 * App
 *
 * @package App
 * @subpackage page
 */
require_once 'App.php';

/**
 * * Register_Index
 *
 * <pre>メンバー登録ページ
 * </pre>
 *
 * @package     App
 * @subpackage  page
 * @author      $Author: moriwaki $
 * @version     $Id: index.php 2008-09-10 moriwaki　$
 */
class Register_Index extends App_Page
{
    /**
     * テンプレート名
     *
     * @var string
     */
    var $template = 'index.tpl';

    /**
     * onInit
     *
     * @param array $args queryString
     * @return void
     * @author moriwaki
     */
    function onInit($args)
    {
    }
    
    /**
     * onOutput
     *
     * @return void
     * @author moriwaki
     */
    function onOutput()
    {
        p('onOutput');
        $this->display($this->template);
    }
    
    /**
     * onAction
     *
     * @param array $submit POSTデータ
     * @return void
     * @author moriwaki
     */
    function onAction($submit)
    {
        p($submit);
        if (isset($submit['finish'])){
            /* 終了 */
            unset($submit['click']);
            unset($submit['finish']);
            $agent = & BEAR_Agent::getInstance();
            if (App::$ua != BEAR_Agent::UA_PC) {
                $submit['device_serial'] = App_Agent::$agent->getDeviceSerial();
            }
            $resource = new BEAR_Resource('/user/profile');
            /* POE(Post Once Exactly)で一度しか実行しない */
            $options['poe'] = true;
            $id = $resource->create($submit, $options);

            $params = array(
              "host" => App::SMTP_HOST,
              "port" => 25,
              "auth" => false
            );
            $mailObject = Mail::factory("smtp", $params);
            $recipients = $submit['mailaddress'];
            $headers = array(
              "To" => $submit['mailaddress'],
              "From" => "moriwaki@gmail.com",
              "Subject" => "Test Mail"
            );
            $body = "This is URL click
http://{$_SERVER['HTTP_HOST']}/register/h/?id={$id}";
            $mailObject -> send($recipients, $headers, $body);
            
            $this->display('index.action.tpl');
        } else {
            /* セッションに保存して次の画面へ */
            $this->display('index.form2.tpl');
            BEAR_Session::merge('submit', $submit);
            $options = array('click' => $submit['click']);
            self::redirect('.', $options);
        }
    }

    /**
     * フォーム1画面
     *
     * @return void
     * @author moriwaki
     */
    public function onClickNone()
    {
        $this->form1();
        $this->buildFormCommon();
    }

    /**
     * フォーム2画面
     *
     * @return void
     * @author moriwaki
     */
    public function onClickForm2()
    {
        $this->template = 'index.form2.tpl';
        $this->form2();
        $this->buildFormCommon();
    }

    /**
     * プレビュー確認画面
     *
     * @return void
     * @author moriwaki
     */
    public function onClickPreview()
    {
        $this->template = 'index.form2.tpl';
        $this->preview();
        $this->buildFormCommon();
    }
    
    /**
     * 共通フォーム
     *
     * @return void
     * @author moriwaki
     */
    private function buildFormCommon()
    {
        $form = & BEAR_Form::getInstance();
        //  フォームインプットフィールド    
        $form->addElement('submit', '_submit', '次へ', '');
        // フィルタと検証ルール
        $form->applyFilter('__ALL__', 'trim');
        $form->applyFilter('__ALL__', 'strip_tags');
    }

    /**
     * フォーム1
     *
     * @return void
     * @author moriwaki
     */
    private function form1()
    {
        $form = BEAR_Form::getInstance();
        $form->addElement('hidden', 'click', 'form2');
        $form->addElement('checkbox', 'ok', '規約に同意します');
        $form->addRule('ok', '規約に同意してください', 'required', null, '');
    }

    /**
     * フォーム2
     *
     * @return void
     * @author moriwaki
     */
    private function form2()
    {
        $form = BEAR_Form::getInstance();
        $form->addElement('hidden', 'click', 'preview');
        require_once('htdocs/my/profile/index.php');
        My_Profile_Index::buildForm();
    }

    /**
     * 確認フォーム
     *
     * @return void
     * @author moriwaki
     */
    private function preview()
    {
        p('preview');
        $this->form2();
        $form = BEAR_Form::getInstance();
        $defaults = BEAR_Session::get('submit');
        $form->setDefaults($defaults);
        $form->addElement('hidden', 'finish', 1);
        $form->freeze();
    }

}

new BEAR_Main('Register_Index');